package cn.edu.jlu.community.app.post;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.edu.jlu.community.R;
import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PostContentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PostContentFragment extends Fragment {

    private static final String ARG_DATA = "param_data";

    private Bundle mData;

    private Drawable mPortrait;

    @InjectView(R.id.title_text)
    protected TextView titleTextView;
    @InjectView(R.id.author_text)
    protected TextView authorTextView;
    @InjectView(R.id.content_text)
    protected TextView contentTextView;
    @InjectView(R.id.time_text)
    protected TextView timeTextView;
    @InjectView(R.id.portrait)
    protected ImageView portraitImage;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ContentFragment.
     */
    public static PostContentFragment newInstance(Bundle data) {
        PostContentFragment fragment = new PostContentFragment();
        Bundle args = new Bundle();
        args.putBundle(ARG_DATA, data);
        fragment.setArguments(args);
        return fragment;
    }

    public PostContentFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mData = getArguments().getBundle(ARG_DATA);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.common_content_fragment, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (null != mData) {
            titleTextView.setText(mData.getString("title", "无标题"));
            authorTextView.setText(mData.getString("author", "无名氏"));
            contentTextView.setText(mData.getString("content", "无内容"));
            timeTextView.setText(mData.getString("time", "时间"));

            if (null != mPortrait) {
                portraitImage.setImageDrawable(mPortrait);
            }
        }
    }

    @OnClick(R.id.show_comment_button)
    public void onClick() {
        String id = (String) mData.get("objId");
        ((MaterialNavigationDrawer) this.getActivity()).setFragmentChild(
                CommentFragment.newInstance(id), "评论");
    }

    public void setPortrait(Drawable drawable) {
        mPortrait = drawable;
    }
}